<template>
    <view>
		<view class="d-kuan">
			<scroll-view class="conversations" scroll-y="true">
			<block data-type="template" data-is="tempCommonList" data-attr="postsList:postsList,listAdsuccess:listAdsuccess">
				<view v-if="postsList.length > 0">
				  <view class="scroll-item" v-for="(conversation, key) in postsList" :key="key">
					<view class="item-head d-yuanjiao-50">
						<image :src="conversation.uid.touxiang"  class="head-icon d-yuanjiao-50"></image>
						<view class="item-head_unread" v-if="conversation.yidu == 0"></view>
					</view>
				  	<view class="scroll-item_info" @click="tiezi(key,conversation.quanziid,conversation.quantieid,conversation.id)">
						<view class="item-info-top">
						  <text class="item-info-top_name d-you-10 d-chaochu-1">{{ conversation.uid.yonghuming }}<text class="d-wenzi-12 d-wenzi-yanse-hongse">: 评论</text></text>
						  <view class="item-info-top_time">{{ conversation.inputtime }}</view>
						</view>
						<view class="item-info-bottom d-buhuanhang">
							
							<view class="item-info-bottom-item ">
								
								<view class="item-info-top_content" >
									
									<u-parse :content="conversation.content"></u-parse>

								</view>
								<view class="item-info-bottom_action" @click.stop="showAction(conversation)"></view>
							</view>
						</view>
				  	</view>
				  </view>
				</view>
				
			</block>
			
			</scroll-view>
			
			<view :style="'display:' + (isLoading?'block':'none')">
				<template is="tempLoading"></template>
			</view>
			<view class="loadingmore" :style="'display:' + floatDisplay">
				<view class="no-more" :hidden="!isLastPage">- 无更多文章 -</view>
			</view>
			<u-divider text="已经到底啦 !" v-if="daodi">{{daodi}}</u-divider>
			<!-- 版权信息template模板 -->
			<view class="copyright" :style="'display:' + floatDisplay">
				<block data-type="template" data-is="tempCopyright" data-attr="webSiteName:webSiteName,domain:domain">
					<view> {{webSiteName}} </view>
					<view> {{banbenhao}} </view>
				</block>
			</view>
		</view>
		<!-- 无法访问网络 -->
		<view class="showerror" :style="'display:' + showerror">
			<!--image src="/static/images/cry80.png" style="height:100rpx;width:100rpx"></image-->
			<view class="errortext">
				暂时无法访问网络，下拉重试...
			</view>
		</view>
	</view>
</template>

<script>
import toubu from "@/components/toubu/toubu";  //引入组件
import Dwaterfall  from "@/components/d-waterfall/d-waterfall";  //引入组件
var Api = require("../../utils/api.js");
var util = require("../../utils/util.js");

var wxApi = require("../../utils/wxApi.js");
var wxRequest = require("../../utils/wxRequest.js");
import config from "../../utils/config";
var pageCount = config.getPageCount;
var webSiteName = config.getWebsiteName;
var domain = config.getDomain;
var topNav = config.getIndexNav;
var banbenhao = config.getDuixinbanbenhao;

export default {
  data() {
    return {
      postsList: [],
	  tubiaoList: [],

      isLastPage: false,
      page: 1,
	  swiperheight: '',
	  swiperItemHeight: '',
      search: '',
      categories: 0,
      showerror: "none",
      showCategoryName: "",
      categoryName: "",
      showallDisplay: "block",
      displayHeader: "none",
      displaySwiper: "none",
      floatDisplay: "none",
      displayfirstSwiper: "none",
      topNav: topNav,
      webSiteName: webSiteName,
      domain: domain,
      isFirst: false,
      // 是否第一次打开,
      isLoading: false,
	  uid:'',
	  zanzhi: false,
	  userInfo:'',
	  //下载加载提示
	  xialajiazai: false,
	  //到底
	  daodi: false,
	  v:config.getDomain+'/static/default/web/img/V.png',
	  banbenhao:banbenhao,
    };
  },
	mounted() {
		
	},


  components: {
	  toubu,
	  Dwaterfall
  },
  props: {},
  onShareAppMessage: function () {
    return {
      title: '“' + webSiteName + '”小程序',
      path: 'pages/index/index',
      success: function (res) {// 转发成功
      },
      fail: function (res) {// 转发失败
      }
    };
  },
  onPullDownRefresh: function () {
    var self = this;
    self.setData({
      showerror: "none",
      showallDisplay: "block",
      displaySwiper: "none",
      floatDisplay: "none",
      isLastPage: false,
      page: 1,

    });

    this.fetchPostsData(self);
  },
  onReachBottom: function () {
    var self = this;
    if (!self.isLastPage) {
      self.setData({
        page: self.page + 1,
		xialajiazai: true,
      });
      console.log('当前页' + self.page);
      this.fetchPostsData(self);
    } else {
      console.log('最后一页');
    }
  },
  onLoad: function (options) {
    uni.setNavigationBarTitle({
      title: '评论消息'
    });

    var self = this;
	var isFirstStorage = uni.getStorageSync('isFirst'); // console.log(isFirstStorage);
	var userInfo = uni.getStorageSync('userInfo');
	self.userInfo = userInfo;

	//console.log("userInfo1",userInfo);
	if (userInfo) {
		self.showuid = true;
		var member = uni.getStorageSync('member');
		var uid = uni.getStorageSync('memberuid');
		self.uid = uid;
		//console.log("member1",self.uid);
		//console.log("userInfo1",userInfo);
		self.fetchPostsData(self);
	} else {
		self.uid = '';
		//self.fetchPostsData(self);
	}


    if (!isFirstStorage) {
      self.setData({
        isFirst: true
      });
      uni.setStorageSync('isFirst', 'no'); // console.log(uni.getStorageSync('isFirst'));
      setTimeout(function () {
        self.setData({
          isFirst: false
        });
      }, 5000);
    }

  },


  onShow() {

    uni.setStorageSync('openLinkCount', 0);

  },
    methods: {

		//获取列表数据
		fetchPostsData: function (data) {
		  var self = this;
		  if (!data) data = {};
		  if (!data.page) data.page = 1;
		  if (!data.categories) data.categories = 0;
		  if (!data.search) data.search = '';
		  if (self.uid) data.uid = self.uid;
		  console.log('uid:',self.uid);

		  if (data.page === 1) {
			self.setData({
			  postsList: []
			});
		  }

		  ;
		  self.setData({
			isLoading: true
		  });
	  

			//var getPostsRequest = wxRequest.getRequest(Api.getRemenlistUrl(data));
			var getTongzhiRequest = wxRequest.getRequest(Api.getTongzhi(self.uid,10,data));
			getTongzhiRequest.then(response => {
				//console.log(response.data.data);
			if (response.data.code == 1) {
				if (response.data.data.length) {
					
				  if (response.data.data.length < pageCount) {
					self.setData({
					  isLastPage: true,
					  isLoading: false
					});
				  }

				  self.setData({
					floatDisplay: "block",
					postsList: self.postsList.concat(response.data.data.map(function (item) {
				
					  if (item.thumb == null || item.thumb == '') {
						item.thumb = "/static/images/logo700.png";
					  }

					  return item;
					}))
				  });
				} else {
				  if (response.data.code == "rest_post_invalid_page_number") {
					self.setData({
					  isLastPage: true,
					  isLoading: false
					});
					uni.showToast({
					  title: '没有更多内容',
					  mask: false,
					  duration: 1500
					});
				  } else {
					  // 隐藏提示
					/*uni.showToast({
					  title: response.data.message,
					  duration: 1500
					});*/
					self.daodi = true;
					uni.showToast({
					  title: '没有更多内容',
					  duration: 500
					});
				  }
				}
			  }
			}).catch(function (response) {
			  if (data.page == 1) {
				self.setData({
				  showerror: "block",
				  floatDisplay: "none"
				});
			  } else {
				uni.showModal({
				  title: '加载失败',
				  content: '加载数据失败,请重试.',
				  showCancel: false
				});
				self.setData({
				  page: data.page - 1
				});
			  }
			}).finally(function (response) {
			  uni.hideLoading();
			  self.setData({
				isLoading: false
			  });
			  uni.stopPullDownRefresh();
			});
		},
		//加载分页
		loadMore: function (e) {
		  var self = this;

		  if (!self.isLastPage) {
			self.setData({
			  page: self.page + 1
			}); //console.log('当前页' + self.data.page);

			this.fetchPostsData(self);
		  } else {
			uni.showToast({
			  title: '没有更多内容',
			  mask: false,
			  duration: 1000
			});
		  }
		},

		//内容帖子
		tiezi(index,quanid,tieid,xid) {
			var duixin = this;
			// console.log('查看帖子');
			
			this.yidu(index,xid); 
			/*this.fetchPostsData(duixin);*/
			var url = '../tiezi/tiezi?qid='+quanid+'&id='+tieid+'&qid='+ duixin.qid;
			uni.navigateTo({
				url: url
			});
		},
		// 已读
		//点赞值
		yidu(index,tieid) {
			var that = this;
			var getYiduRequest = wxRequest.getRequest(Api.getYidus(tieid,that.uid,that.userInfo));
			var res;
			
			getYiduRequest.then(response => {
				res = response;
				if(res.data.code) {
					this.postsList[index].yidu = 1;
				} else {

				  uni.showModal({
				  	title: '请先登录',
				  	//content: '请先登录',
				  	confirmText: '前往登录',
				  	success: function(res) {
				  		if (res.confirm) {
				  			uni.redirectTo({
				  				url: '/pages/login/login'
				  			});
				  		}
				  	}
				  });
				  // uni.reLaunch({ url: '/pages/login/login'}) // 跳转到登录页
				}
			})
		},
    },
	computed: {
		
	},

};
</script>
<style lang="scss">
@import "./xiaoxilist.css";
</style>